package com.momo.hotel.controller;

import com.momo.hotel.dao.pojo.TbUser;
import com.momo.hotel.dao.pojo.ext.TbShopCarExt;
import com.momo.hotel.dao.pojo.ext.TotalPrice;
import com.momo.hotel.service.ShopCarService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author qiang
 * @create 2019/7/5
 */
@Controller
public class ShopCarController {
    @Resource
    ShopCarService shopCarService;
    /**
     * 酒店购物车
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("shopCar")
    public Object shopCar(HttpServletRequest request){
        TbUser user = (TbUser) request.getSession().getAttribute("user");
        if (user!=null){
            List<TbShopCarExt> tbShopCarExts = shopCarService.selectShopCar(user.getId());
            TotalPrice totalPrice = new TotalPrice();
            double total = 0;
            for (int i = 0 ; i < tbShopCarExts.size(); i++){
                total+=tbShopCarExts.get(i).getRoomPrice()+tbShopCarExts.get(i).getPledge();
            }
            totalPrice.setTotalPrice(total);
            totalPrice.setTbShopCarExt(tbShopCarExts);
            request.getSession().setAttribute("totalPrice",totalPrice);
            return tbShopCarExts;
        }else {
            return "请登录";
        }
    }

    /**
     * 删除购物车
     * @return
     */
    @RequestMapping("deleteShopCar")
    public String deleteShopCar(HttpServletRequest request){
        String id = request.getParameter("id");
        Integer integer = shopCarService.deleteShopCar(Integer.parseInt(id));
        if(integer != 0){
            return "shop-cat";
        }else {
        return "shop-cat";
        }
    }
}
